import collections


class Solution:
    def minAreaRect(self, points: list) -> int:
        res = 2**31
        x = collections.defaultdict(list)
        y = collections.defaultdict(list)
        pos = set()
        for point in points:
            x[point[0]] += [point]
            y[point[1]] += [point]
            pos.add(tuple(point))
        for point in points:
            for x_point in x[point[0]]:
                if x_point[1] <= point[1]:
                    continue
                for y_point in y[point[1]]:
                    if y_point[0] <= point[0]:
                        continue
                    if (y_point[0], x_point[1]) in pos:
                        length = abs(point[0] - y_point[0])
                        width = abs(point[1] - x_point[1])
                        res = min(res, length * width)

        return 0 if res == 2**31 else res
